package com.yh.ssm.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.ssm.common.CodeMsg;
import com.yh.ssm.common.Result;
import com.yh.ssm.domain.TReply;
import com.yh.ssm.domain.TTopic;
import com.yh.ssm.form.TreplyForm;
import com.yh.ssm.mapper.TTopicMapper;
import com.yh.ssm.query.TreplyQuery;
import com.yh.ssm.query.TtopicQuery;
import com.yh.ssm.service.TReplyService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.yh.ssm.mapper.TReplyMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: TReplyServiceImpl.java
 * @author: yuhui
 * @version: 1.0.0
 * @Description: TODO
 * @createTime: 2021年04月29日 09:27:00
 */
@Service
public class TReplyServiceImpl implements TReplyService {

    @Resource
    private TReplyMapper tReplyMapper;
    @Autowired
    private TTopicMapper tTopicMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addInfo(TreplyForm treplyForm) {
        //1.先查看这个贴纸是否存在
        List<TTopic> tTopics = tTopicMapper.selectData(new TtopicQuery(treplyForm.getTopicid(), null, null));
        if(tTopics != null && tTopics.size() == 0 || tTopics == null){
            return new Result(CodeMsg.TOPIC_IS_NOTHAVE);
        }
        //2.再添加评论
        tReplyMapper.addData(treplyForm);
        return new Result();
    }

    @Override
    public Result getInfoByTopicID(TreplyQuery treplyQuery) {
        Page<TReply> objects = PageHelper.startPage(treplyQuery.getPage(), treplyQuery.getLimit());
        tReplyMapper.selectInfoByTopic(treplyQuery);
        return new Result(objects.toPageInfo());
    }
}
